package cn.virens.web.components.shiro.spring;

import java.util.Objects;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;

/**
 * Shiro 使用 Spring 缓存管理器
 * 
 * @作者   :loioi
 * @创建时间 :2017年7月17日
 */
public class SpringCacheManage implements CacheManager {
	private org.springframework.cache.CacheManager mCacheManager;

	/**
	 * 注入Spring的缓存管理器
	 * 
	 * @param cacheManager
	 */
	public SpringCacheManage(org.springframework.cache.CacheManager cacheManager) {
		this.mCacheManager = cacheManager;
	}

	/**
	 * 获取 缓存
	 */
	@Override
	public <K, V> Cache<K, V> getCache(String name) throws CacheException {
		Objects.requireNonNull(mCacheManager, "Spring CacheManager is not load!");

		try {
			return new SpringCache<>(mCacheManager.getCache(name));
		} catch (Throwable t) {
			throw new CacheException(t);
		}
	}
}
